<?php

namespace app\index\model;

use think\Model;

class Commonmodel extends Model{

    /**
     * 保存\修改数据
     * @param array $data         保存\修改数据
     * @param string $id          内容ID
     * @return boolean|string
     */
    public function saveDataInfo($data, $id = null){
        //$res = Db::table($this->table)->fetchSql(true)->select();
        //$res = self::db()->fetchSql(true)->select();

        if(!count($data))  return false;

        try{
            if($id){
                //修改
                self::db()->where($this->pk, $id)->update($data);
            }else{
                //新增
                self::db()->save($data);
            }
        }catch(\Exception $e){
            trace('(saveDataInfo)保存\修改失败，添加内容为：' . json_encode($data) . '失败原因：' . $e->getMessage(), 'error');
            return false;
        }

        if(!$id)  $id = self::db()->getLastInsID();
        return $id;
    }

    /**
     * 不分页查询所有
     * @param string $select        查询字段
     * @param string $where         查询条件
     * @return boolean|unknown
     */
    public function findAllInfo($select=  "*", $where = null, $order = null){

        $whereData = [['is_delete' , '=', 1]];

        if($where)  $whereData = array_merge($where, $whereData);

        $list = self::db()->field($select)->where($whereData)->order($order)->select()->toArray();

        if(!count($list))  return false;
        return $list;
    }

    /**
     * 分页查询
     * @param string $select
     * @param null $where
     * @param null $order
     * @param int $num
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public function findAllPageInfo($select=  "*", $where = null, $order = null, $pageParam = null, $num = 15){
        $whereData = [['is_delete' , '=', 1]];
        $paginate = ['list_rows'=>$num];

        if($where)  $whereData = array_merge($where, $whereData);
        if($pageParam && $pageParam = array_splice($pageParam, 1))  $paginate = array_merge($paginate, ['query'=>$pageParam]);

        $data = self::db()->field($select)->where($whereData)->order($order)->paginate($paginate);

        if(!$data)  return false;

        $list['page'] = $data->render();
        $dataArr = $data->toArray();
        $list['data'] = $dataArr['data'];

        return $list;
    }

    /**
     * 根据ID获取内容详情信息
     * @param string $select
     * @param null $id
     * @param null $where
     * @return array|bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function findDetaiInfoById($select=  "*", $id = null, $where = null){

        if(!$id && !$where)  return false;

        if($id)  $whereData = array($this->pk=>$id);

        if($where && $whereData)  $whereData = array_merge($where, $whereData);

        $list = self::db()->field($select)->where($whereData)->find()->toArray();

        if(!count($list))  return false;
        return $list;
    }

    /**
     * 根据其他条件查询单条信息
     * @param string $select
     * @param null $where
     * @return array|bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function findDetaiInfoByOthers($select=  "*", $where = null){

        if(!$where)  return false;

        $list = self::db()->field($select)->where($where)->select()->toArray();

        if(!count($list))  return false;
        return $list;
    }

    /**
     * 删除
     * @param $id
     * @return bool|string
     */
    public function del($id){
        if(!$id)  return false;

        $res = $this->saveDataInfo(['is_delete'=>2], $id);

        if(!$res)  return false;
        return $res;
    }

}

?>